package com.wondertek.common.util.base;

import cn.hutool.core.util.StrUtil;
import cn.hutool.http.HttpException;
import cn.hutool.http.HttpRequest;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author: gongjiangtao
 * @date: 2024/9/9 11:17
 * @Description: 语音通知工具类
 * @Version: 1.0
 */
@Configuration
@Slf4j
public class VoiceNoticeUtil {

//    private static final String VOICE_NOTICE_URL = "http://api.nxcloud.com/api/voiceSms/notsend";
//    private static final String APP_KEY = "FUKOKfdf";
//    private static final String SECRET_KEY = "6BSaK9Dr";

    private static String domain;

    private static String appKey;

    private static String secretKey;

    @Value("${voice.url:http://api.nxcloud.com/api/voiceSms/notsend}")
    public void setDomain(String domain) {
        VoiceNoticeUtil.domain = domain;
    }

    @Value("${voice.app-key:FUKOKfdf}")
    public void setAppId(String appKey) {
        VoiceNoticeUtil.appKey = appKey;
    }

    @Value("${voice.secret-key:6BSaK9Dr}")
    public void setAppKey(String secretKey) {
        VoiceNoticeUtil.secretKey = secretKey;
    }

    public static boolean send(String code, List<String> phone, String content) {
        // 构建请求参数
        Map<String, Object> paramMap = new HashMap<>();
        paramMap.put("appkey", appKey);
        paramMap.put("secretkey", secretKey);
        paramMap.put("phone", StrUtil.join(",", phone));
        paramMap.put("country_code", code);
        paramMap.put("show_phone", "8613977777777");
        paramMap.put("content", content);
        paramMap.put("lang", "en");

        log.info("语音通知请求参数,{}", JSONUtil.toJsonStr(paramMap));
        // 发送 POST 请求
        try {
            String result = HttpRequest.post(domain).form(paramMap).timeout(5000).execute().body();
            log.info("语音通知请求结果,{}", JSONUtil.toJsonStr(result));
            JSONObject jsonObject = JSONUtil.parseObj(result);
            if (StrUtil.equals("0", jsonObject.getStr("code")) && StrUtil.equals("Success", jsonObject.getStr("message"))) {
                return true;
            }
        } catch (HttpException e) {
            log.error("语音通知请求异常", e);
        }
        return false;
    }
}
